package com.zxf.appliction.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zxf.appliction.dao.ArticleMapper;
import com.zxf.appliction.dao.ContentMapper;
import com.zxf.appliction.domain.Article;
import com.zxf.appliction.domain.Content;
import com.zxf.appliction.domain.Params;

@Service
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class ArticleService {

	@Autowired
	private ArticleMapper articleMapper;

	@Autowired
	private ContentMapper contentMapper;

	public List getArticleList() {
		return articleMapper.getArticleallList();
	}

	/**
	 * 添加文章信息
	 * 
	 * @param article
	 * @param content 
	 * @return
	 */
	public int savearticle(Article article, Content content) {
		// 如果文章id号不为空，则是更新
		int code = 0;
		if (null != article.getArticleid()) {
			code = articleMapper.uparticle(article);
			if (code > 0) {
				content.setArticleid(article.getContentid());
				code = contentMapper.upcontent(content);
			}
		} else {
			code = articleMapper.insertSelective(article);
			if (code > 0) {
				code = contentMapper.insertSelective(content);
			}
		}

		return code;

	}

	// 保存文章内容
	public int savecontent(Content content) {

		return contentMapper.insert(content);

	}

	public List getArticleList(Integer channelID, Integer limit) {
		return articleMapper.getArticleListindex(channelID, limit);
	}

	public int getartcletotle(Params params, Article article) {
		HashMap paramsmap = new HashMap();
		paramsmap.put("start", params.getStart());
		paramsmap.put("limit", params.getLimit());
		paramsmap.put("articleID", article.getArticleid());
		paramsmap.put("typeid", article.getTypeid());
		paramsmap.put("channelid", article.getChannelid());
		paramsmap.put("title", article.getTitle());
		paramsmap.put("author", article.getAuthor());
		paramsmap.put("source", article.getSource());
		paramsmap.put("startdate", params.getStartdate());
		paramsmap.put("enddate", params.getEnddate());
		return articleMapper.getartcletotle(paramsmap);
	}

	
	/**
	 * 带搜索参数的
	 * @param params 
	 * @param article 
	 * @return list 集合
	 */
	public List getArticleList(Params params, Article article) {
		// 开始记录数
		Integer starnum = params.getLimit() * (params.getPage() - 1);
		params.setStart(starnum);
		HashMap paramsmap = new HashMap();
		paramsmap.put("start", params.getStart());
		paramsmap.put("limit", params.getLimit());
		paramsmap.put("articleID", article.getArticleid());
		paramsmap.put("typeid", article.getTypeid());
		paramsmap.put("channelid", article.getChannelid());
		paramsmap.put("title", article.getTitle());
		paramsmap.put("author", article.getAuthor());
		paramsmap.put("source", article.getSource());
		paramsmap.put("startdate", params.getStartdate());
		paramsmap.put("enddate", params.getEnddate());
		return articleMapper.getArticleList(paramsmap);
	}

	// 删除文章
	public int delarticle(int i) {
		return articleMapper.deleteByPrimaryKey(i);
	}

	public int delcontent(String title) {
		return contentMapper.delcontent(title);
	}

	// 查看详细信息
	public Article detailarticle(int parseInt) {
		return articleMapper.selectByPrimaryKey(parseInt);
	}

	// 查看详细信息
	public Content detailcontent(Integer artid) {
		return contentMapper.detailcontent(artid);
	}

	public int uparticle(Article article) {
		// 修改信息
		return articleMapper.uparticle(article);
	}

	public int upcontent(Content content) {
		// 修改信息
		return contentMapper.upcontent(content);
	}

	

	// 上一篇
	public Article findPrevious(int artid, int typeid) {
		HashMap paramsmap = new HashMap();
		paramsmap.put("artid", artid);
		paramsmap.put("typeid", typeid);
		return articleMapper.findPrevious(paramsmap);
	}

	// 下一篇
	public Article findNextarticle(int artid, int typeid) {
		HashMap paramsmap = new HashMap();
		paramsmap.put("artid", artid);
		paramsmap.put("typeid", typeid);
		return articleMapper.findNextarticle(paramsmap);
	}

	/**
	 * 查询首页我文章数量
	 * 
	 * @param params  params
	 * @param searchText 检索词
	 * @return 文章集合list
	 */
	public List<Article> getArticleListByPagesizeAndPage(Params params, String searchText) {
		// 开始记录数
		Integer starnum = params.getPagesize() * (params.getPage() - 1);
		Integer endnum = params.getPagesize();

		return articleMapper.getArticleListByPagesizeAndPage(starnum, endnum, searchText);

	}

	/**
	 * 查询文章总数
	 * 
	 * @param params params
	 * @param searchText 
	 * @return 记录总页数
	 */
	public int getArtcleTotleByPagesizeAndPage(Params params, String searchText) {
		// 开始记录数
		Integer starnum = params.getPagesize() * (params.getPage() - 1) + 1;
		Integer endnum = starnum + params.getPagesize();

		int totalArticle = articleMapper.getArtcleTotleByPagesizeAndPage(
				starnum, endnum, searchText);
		int totalPages = 0;
		if (totalArticle % params.getPagesize() != 0) {
			totalPages = totalArticle / params.getPagesize() + 1;
		} else {
			totalPages = totalArticle / params.getPagesize();
		}
		return totalPages;
	}
    /**
     * 单独添加文章信息
     * @param article
     * @return int
     */
	public int savearticle(Article article) {
		return articleMapper.insertSelective(article);
	}
	
    /**
     * 幻灯片列表
     * @param i 5个
     * @return list集合
     */
	public List<Article> getArticleListBytop(Integer i) {
		return articleMapper.getArticleListBytop(i);
	}


}
